package dm;

import java.sql.CallableStatement;
import java.sql.Connection;

import database.DBConnection;

public class ManagerDM {

	public void addFirm(String name, String desc) throws Exception{
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call FIRM_PKG.ADD_FIRM(?,?)}");
			statement.setString(1, name);
			statement.setString(2,desc);
			statement.execute();
			
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}
	
	public void updateFirm(int id, String name, String desc) throws Exception{
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call FIRM_PKG.UPDATE_FIRM(?,?,?)}");
			statement.setInt(1,id);
			statement.setString(2,name);
			statement.setString(3,desc);
			statement.execute();
						
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}

	
	public void deleteFirm(int id) throws Exception{
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call FIRM_PKG.DELETE_FIRM(?)}");
			statement.setInt(1,id);
			statement.execute();
						
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}

	public void insertProduct(int productId,double price,int count) throws Exception
	{
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call PRODUCT_PKG.INSERT_PRODUCT(?,?,?)}");
			statement.setInt(1,productId);
			statement.setDouble(2,price);
			statement.setInt(3,count);
			statement.execute();
			
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
		
		
		
	}
	
	public void addProduct(String name, String brand,String description, int groupId, String categoryId, double kdv) throws Exception
	{
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call PRODUCT_PKG.ADD_PRODUCT(?,?,?,?,?,?)}");
			statement.setString(1,name);
			statement.setString(2,brand);
			statement.setString(3,description);
			statement.setInt(4, groupId);
			statement.setString(5, categoryId);
			statement.setDouble(6,kdv);
			statement.execute();
			
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}
	
	public void updateProduct(int id, String name, String brand, 
			String description, int groupId, String categoryId, double kdv) throws Exception{
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call PRODUCT_PKG.UPDATE_PRODUCT(?,?,?,?,?,?,?)}");
			statement.setInt(1, id);
			statement.setString(2,name);
			statement.setString(3,brand);
			statement.setString(4,description);
			statement.setInt(5, groupId);
			statement.setString(6, categoryId);
			statement.setDouble(7,kdv);
			statement.execute();
						
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}

	public void deleteProduct(int id) throws Exception{
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;
		try {
			statement = con.prepareCall("{call PRODUCT_PKG.REMOVE_PRODUCT(?)}");
			statement.setInt(1,id);
			statement.execute();
						
			statement.close();
			DBConnection.closeConnection(con);
			
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}
}
